package com.xiaotu.spider.project.pageprocessor.list;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 SohuListPageProcessor
 * @日期 2017年8月10日
 * @作者 王艳龙
 * @功能 优酷网站2015-2017数据爬取
 */

@Component("YouKuListPageProcessor")
@Scope("prototype")
public class YouKuListPageProcessor extends AUrlPageProcessor{
	
	private String fileName; //存放文件路径
	
	private static String [] YEARS = new String[3];//最近三年热播剧
	
	private static String one_url_0 = "http://list.youku.com/category/show/c_96_g_%E7%BD%91%E7%BB%9C%E5%A4%A7%E7%94%B5%E5%BD%B1_r_";
	private static String one_url_1 = "_u_1_s_1_d_4.html?spm=a2h1n.8251845.filterPanel.5!4~1~3!4~A";
	
	private static String two_url_0 = "http://list.youku.com/category/show/c_96_g_%E7%BD%91%E7%BB%9C%E5%A4%A7%E7%94%B5%E5%BD%B1_r_";
	private static String two_url_1 = "_u_1_s_1_d_4.html?spm=a2h1n.8251845.filterPanel.5!4~1~3!3~A";
	
	private static String three_url_0 = "http://list.youku.com/category/show/c_96_g_%E7%BD%91%E7%BB%9C%E5%A4%A7%E7%94%B5%E5%BD%B1_r_";
	private static String three_url_1 = "_s_1_d_4_u_1.html?spm=a2h1n.8251845.filterPanel.5!5~1~3!2~A";
	
	static {
		String[] recentYears = DateUtils.getRecentYears(3);
		for(int i = 0;i<recentYears.length;i++){
			if(i==0){
				YEARS[i] = one_url_0+recentYears[i]+one_url_1;
			}else if(i==1){
				YEARS[i] = two_url_0+recentYears[i]+two_url_1;
			}else if(i==2){
				YEARS[i] = three_url_0+recentYears[i]+three_url_1;
			}
		}
	}
	
	@Override
	protected String[] getStartRequestUrls()
	{
		return YEARS;
	}
	
	 @Override
	    public void initRunParams(Map<String, Object> map)
	    {
	        if (map.get("fileName") == null)
	            throw new SpiderException(
	                    SpiderExceptionCode.PageProcessorParameterErrorException);
	        fileName = map.get("fileName") + StringUtils.EMPTY;
	    }
	
	@Override
	public String getPageDataType(Page page){
		String url = page.getUrl().toString();
        if (url.contains("http://list.youku.com/category")){
        	 return Constants.PageDataType.SEARCH_LIST;
        }else if(url.contains("http://v.youku.com/v_show")){
        	 return Constants.PageDataType.TV_RECORD;
        }else if(url.contains("http://list.youku.com/show")){
        	 return Constants.PageDataType.TV_PRO_COMMENT;
        }else{
        	 return Constants.PageDataType.SEARCH_LIST;
        }
        
	}
	
	@Override
	public void process(Page page){
		Map<String, Object> tvMap = page.getRequest().getExtras();
		switch (this.getPageDataType(page)){
            case Constants.PageDataType.SEARCH_LIST:
                this.searchProcess(page);
                break;
            case Constants.PageDataType.TV_RECORD:
            	this.detailsPlayProcess(page,tvMap);
            	break;
            case Constants.PageDataType.TV_PRO_COMMENT:
                this.detailsProcess(page,tvMap);
                break;
            default:
                break;
	     }
	}
	
	public void searchProcess(Page page){
		Map<String, Object> tvMap = page.getRequest().getExtras();
		List<String> divList = page.getHtml()
				.xpath("//div[@class='box-series']/ul[@class='panel']/li")
				.all();
		if (divList == null || divList.isEmpty())
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		for (String divStr : divList){
			Html divObj = new Html(divStr);
			String links =divObj.xpath("//div[@class='yk-pack pack-film']/div[@class='p-thumb']/a/@href").get();
			tvMap.put("linkUrl", links);
            PageUtils.addTargetRequest(page, links, page.getRequest().getExtras());
		}
		
		String current = page.getHtml()
				.xpath("//ul[@class='yk-pages']/li[@class='current']/span/text()")
				.get();
		if (StringUtils.isNotEmpty(current))
		{
			current = RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, current);
			if (StringUtils.isNotEmpty(current))
			{
				if (PropertiesUtil.getPropertyByInt(
						Constants.SpiderConfigKey.HOTTV_PAGE_SIZE) > Integer
								.parseInt(current))
					PageUtils.addTargetRequest(page,
							page.getHtml()
									.xpath("//ul[@class='yk-pages']/li[@class='next']/a/@href")
									.get(),
							page.getRequest().getExtras());
			}
		}
	}
	
	
	
	//电影详情播放页
	public void detailsPlayProcess(Page page,Map<String, Object> tvMap){
		String links = page.getHtml().xpath("//a[@class='desc-link']/@href").get();
		PageUtils.addTargetRequest(page, links, page.getRequest().getExtras());
	}
	
	//电影详情页
	public void detailsProcess(Page page,Map<String, Object> tvMap){
		StringBuffer buffer = new StringBuffer();
		//网大名称
		String tvName = page.getHtml()
				.xpath("//div[@class='p-base']/ul/li[@class='p-row p-title']/text()").get();
		if(StringUtils.isNotEmpty(tvName)){
			tvName = tvName.replace("：", "");
			buffer.append(tvName);
		}
		//上映时间
		String movieDate =page.getHtml().xpath("//div[@class='p-base']/ul/li/span[@class='pub']/text()").get();
		buffer.append(SepratorUtil.SEP_PIPE+movieDate);
		//年份
		String movieTime = page.getHtml().xpath("//span[@class='sub-title']/text()").get();
		buffer.append(SepratorUtil.SEP_PIPE+movieTime);
		
		List<String> movieList =page.getHtml().xpath("//div[@class='p-base']/ul/li").all();
		for (String str : movieList) {
			Html htmlStr =new Html(str);
			String spanType = htmlStr.xpath("//li/text()").get();
			 if(spanType.contains("地区")){
				List<String> movieAreas =htmlStr.xpath("//li/a/text()").all();
				StringBuffer sub = new StringBuffer();
				if(movieAreas.size()>0){
					for (String areas : movieAreas) {
						if(StringUtils.isNotEmpty(areas)){
							if(movieAreas.size()>1){
								sub.append(areas+SepratorUtil.SEP_SLASH_EN);
							}else{
								sub.append(areas);
							}
						}
					}
						String movieCounty =sub.toString(); 
						buffer.append(SepratorUtil.SEP_PIPE+movieCounty);
				}
			}else if(spanType.contains("类型")){
				List<String> movieTypes =htmlStr.xpath("//li/a/text()").all();
				StringBuffer sub = new StringBuffer();
				if(movieTypes.size()>0){
					for (String types : movieTypes) {
						if(StringUtils.isNotEmpty(types)){
							if(movieTypes.size()>1){
								sub.append(types+SepratorUtil.SEP_SLASH_EN);
							}else{
								sub.append(types);
							}
						}
					}
					String movieTyp =sub.toString(); 
					buffer.append(SepratorUtil.SEP_PIPE+movieTyp);
				}
			}else if(spanType.contains("总播放数")){
				if(StringUtils.isNotEmpty(spanType)){
					String playCount =spanType.replace("总播放数：", "");
					buffer.append(SepratorUtil.SEP_PIPE+playCount);
				}
			}
		}
		//时长
		String movieTImeLen =page.getHtml().xpath("//ul[@class='p-info p-bottom']/li/span[@class='p-time']/span/text()").get();
		buffer.append(SepratorUtil.SEP_PIPE+movieTImeLen);
		
		//评分
		String movieScore =page.getHtml().xpath("//li[@class='p-score']/span[@class='star-num']/text()").get();
		buffer.append(SepratorUtil.SEP_PIPE+movieScore);
		//影片简介
		String moviePresents = page.getHtml().xpath("//span[@class='intro-more']/text()").get();
		if(StringUtils.isNotEmpty(moviePresents)){
			String moviePresent =moviePresents.replace("简介：", "");
			buffer.append(SepratorUtil.SEP_PIPE+moviePresent);
		}
		//网址
		buffer.append(SepratorUtil.SEP_PIPE+tvMap.get("linkUrl"));
		
		//输出csv文件
		FileUtil.writeFile(fileName, buffer.toString()
	                + SepratorUtil.LINE_SEPRATOR);
	}
	
	@Override
	protected String getSpiderType()
	{
		return "YouKuList";
	}
}
